package com.yst.shader.feature.shimmer

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.core.view.children
import androidx.fragment.app.Fragment
import com.facebook.drawee.backends.pipeline.Fresco
import com.yst.shader.R
import com.yst.shader.ext.hoverOnFocusInChildren
import com.yst.shader.feature.widget.CanvasShimmerView
import kotlinx.android.synthetic.main.fragment_shader_test.view.*

/**
 * @author: xuyujian@ysten.com
 *  @date: 2020/8/17 10:26
 */
class ShaderTestFragment : Fragment(R.layout.fragment_shader_test) {


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (view.ll as ViewGroup).hoverOnFocusInChildren()

        view.v2.hierarchy.setPlaceholderImage(R.drawable.test)
        view.v4.hierarchy.setPlaceholderImage(R.drawable.test)
        view.v5.hierarchy.setPlaceholderImage(R.drawable.test)

        view.v6.hierarchy .setPlaceholderImage(R.drawable.test)
        view.v7 .hierarchy.setPlaceholderImage(R.drawable.test)

        view.ll.children.forEach {
            it.setOnFocusChangeListener { v, hasFocus ->
                if(v ==view.fr_7){
                   if (hasFocus){
                       view.fr_7.background = GradientDrawable().apply {
                           setStroke(15,Color.RED)
                           cornerRadius = 10f
                       }
                   }else{
                       view.fr_7.background = null
                   }
                    v.v7?.onFocused =  hasFocus
                    v.v7?.postInvalidate()
                }
                (v as? CanvasShimmerView)?.run {
                    onFocused = hasFocus
                }
                if (hasFocus) {
                    v.animate().scaleX(1.2f).scaleY(1.2f).setStartDelay(100).start()
                } else {
                    v.animate().scaleX(1f).scaleY(1f).start()
                }
            }
        }


    }

    private fun newController() = Fresco.newDraweeControllerBuilder()
        .setUri("http://p0.ssl.qhimgs1.com/sdr/400__/t010e7771b60b3ad896.gif")
        .setAutoPlayAnimations(true) // 设置加载图片完成后是否直接进行播放
        .build();
}